//index.js
//获取应用实例
const app = getApp()
const api = require('../../utils/api.js');
Page({
	data: {
		redList: [],
		pageNum:1,
		total:0,
		selectId:"",
		money:0,
		windowWidth: app.systemInfo.windowWidth,
		windowHeight: app.systemInfo.windowHeight,
		pixelRatio: app.systemInfo.pixelRatio,
	},
	onLoad: function(options) {
		this.redList(app.globalData.userId,this.data.pageNum,options.coursePoint);
		if(options.redBagId != null){
			this.setData({
				selectId: options.redBagId
			});
		}
	},
	
	redList:function(ownerId,pageNum,coursePoint){
		let _this = this;
		api.redList({
			data: {
				ownerId: ownerId,
				pageSize: "20",
				pageNum: pageNum,
				coursePoint:coursePoint,
				bagStatus:"0"
			},
			success: function(res) {
				if(res.data.code == 0) {
					let list = _this.data.redList;
					res.data.list.rows.forEach(function(item){
						item.endTime = item.endTime.substring(0,10);
						list.push(item);
					});
					_this.setData({
						redList: list,
						pageNum:pageNum,
						total:res.data.list.total
					});
				}
			},
		});
	},
	searchScrollLower(){
		let _this = this;
		if(this.data.total > this.data.pageNum * 10){
			setTimeout(function(){
				_this.setData({
					pageNum: this.data.pageNum + 1
				});
				_this.redList(app.globalData.userId,_this.data.pageNum);
			},500);
		}else{
			console.log(this.data.pageNum)
		}
	},
	toRedInfo(){
		wx.navigateTo({
			url: '../fetchred/redinfo'
		})
	},
	selectRed:function(e){
		this.setData({
			selectId: e.currentTarget.dataset.id,
			money:e.currentTarget.dataset.money
		});
		console.log(e.currentTarget);
	},
	//确定选择红包
	backOrder(){
		var pages = getCurrentPages();
		var prevPage = pages[pages.length - 2];  //上一个页面
		//直接调用上一个页面的setData()方法，把数据存到上一个页面中去
		prevPage.setData({
			redBagId: this.data.selectId,
			redBagMoney:this.data.money
		})
		//返回前一页
		wx.navigateBack({
			delta:1
		})
	},
})